package cn.com.server.exception;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import cn.com.server.common.util.ExceptionUtil;

@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	
	private Logger logger = Logger.getLogger(GlobalDefaultExceptionHandler.class);
	
    /**
     *返回视图：
     *定义一个ModelAndView即可，
     *然后return;
     *定义视图文件;
     *服务器 500页面 ;
     */
	@ExceptionHandler(value = Exception.class)
    public String defaultErrorHandler(HttpServletRequest req, Exception e) {
		//打印异常信息：
		logger.debug("===================");
		logger.debug(ExceptionUtil.exceptionToString(e));
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.addObject("url", req.getRequestURL());
        mav.setViewName("error");
        return "500";
	}

	
	/**
	 * 也可以返回一串字符串
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
    public String defaultErrorHandler(HttpServletRequest req, Exception e) {
		//打印异常信息：
        System.out.println("===================");
        return "sss";
	}
	*/
	
	
}
